package com.kexun.lxj;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockDemo01 {
    public static void main(String[] args) {
        Runnable run = new Run();
        Thread t1 = new Thread(run);
        Thread t2 = new Thread(run);
        Thread t3 = new Thread(run);
        Thread t4 = new Thread(run);
        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }
}


class Run implements Runnable {
    private int i = 100;

    Lock lock=new ReentrantLock();//lock接口的实现类
    @Override
    public void run() {



        while (true) {

            lock.lock();
            try {
                Thread.sleep(50);
                if (i > 0) {
                    System.out.println("这是" + Thread.currentThread().getName() + "卖出的第" + i-- + "张票");
                    Thread.sleep(50);

                } else {
                    System.out.println(Thread.currentThread().getName() + "卖完了,关闭卖票窗口");
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }


        }
    }
}